package com.edou.member.controller.admin;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.base.token.TokenValidate;
import com.edou.member.entity.MemberCollection;
import com.edou.member.entity.MemberUser;
import com.edou.member.form.MemberCollectionForm;
import com.edou.member.service.IMemberCollectionService;
import com.edou.member.service.IMemberUserService;



/**
 * 会员收藏
 * @creator     
 * @create-time 2016/4/18 17:15:31
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("memberCollectionAdminController")
@Scope("prototype")
@RequestMapping("/admin/member/collection")
public class MemberCollectionAdminController extends BaseController {

	@Resource(name = "memberCollectionServiceImpl")
	private IMemberCollectionService memberCollectionService;
	
	@Resource(name = "memberUserServiceImpl")
	private IMemberUserService memberUserService;
	


	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/member/member_collection_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listMemberCollectionByPage.htm", method = RequestMethod.POST)
	public void listMemberCollectionByPage(@ModelAttribute MemberCollectionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			memberCollectionService.listMemberCollectionByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","memberUser","objectId","objectType","memberUserName","objectName"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询会员收藏时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 保存会员收藏
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute MemberCollectionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/member/member_collection_add");
		Map<String,Object> data = view.getModel();
		if(model.getItem().getId() != null){
		MemberCollection item = memberCollectionService.getMemberCollectionById(model.getItem().getId());
			data.put("item", item);
		}
		
		//查询所有会员基本信息
		List<MemberUser> memberUserList = memberUserService.listMemberUserAll();
		data.put("memberUserList",memberUserList);
		


		return view;
	}

	/**
	 * 保存会员收藏
	 * @return 
	 */
	@RequestMapping(value = "/saveMemberCollection.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveMemberCollection(@ModelAttribute MemberCollectionForm model,HttpServletRequest request,HttpSession session){
		try {
			if(model.getItem().getId() != null){
				memberCollectionService.add(model.getItem());
			}else{
				memberCollectionService.update(model.getItem());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存会员收藏时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除会员收藏
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute MemberCollectionForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			memberCollectionService.delByIds(model.getSelIds().split(","));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除会员收藏时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}

}